package com.jiukuaidao.zhulin.module.address;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.entity.ApiCityListInfo;
import com.jiukuaidao.zhulin.entity.ApiCityListInfo.CityList;

import org.greenrobot.eventbus.EventBus;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/13
 * Description :
 */
public class CityListTopPopup extends PopupWindow {
    private View mMenuView;
    private Activity mActivity;
    private LinearLayout mPopup;
    private XRecyclerView mCityRecycle;
    private ApiCityListInfo mCityListInfo;
    private BaseXrecyclerViewAdapter mBaseAdapter;

    public CityListTopPopup(Activity activity, ApiCityListInfo cityListInfo) {
        this.mActivity = activity;
        this.mCityListInfo = cityListInfo;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_city_list, null);
        mPopup = (LinearLayout) mMenuView.findViewById(R.id.popup);
        mCityRecycle = (XRecyclerView) mMenuView.findViewById(R.id.city_recycle);
        mCityRecycle.setLayoutManager(new LinearLayoutManager(mActivity));
        mCityRecycle.setPullRefreshEnabled(false);
        mCityRecycle.setLoadingMoreEnabled(false);
        mBaseAdapter = new BaseXrecyclerViewAdapter<CityList>(mActivity, R.layout.item_city_list, mCityListInfo.cityList, mCityRecycle) {


            @Override
            public void updateUI(BaseHolder holder, int position, ApiCityListInfo.CityList data) {
                TextView cityName = holder.getView(R.id.item_city_name);
                cityName.setText(data.cityName);
            }

            @Override
            protected void onItemClick(BaseHolder holder, int position) {
                super.onItemClick(holder, position);
                ApiCityListInfo.CityList cityList = mCityListInfo.cityList.get(position);
                EventBus.getDefault().post(cityList);
            }
        };
        mCityRecycle.setAdapter(mBaseAdapter);

        // 设置popwindow的View
        this.setContentView(mMenuView);
        // 设置popwindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置popwindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = mPopup.getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y > height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        /** 执行弹出动画 **/
        TranslateAnimation anim =  new TranslateAnimation(0, 0, -300, 0);
        anim.setDuration(300);
        anim.setFillAfter(false);
        mPopup.startAnimation(anim);
    }
}
